package com.xxl.job.admin.service.impl;

import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.xxl.job.admin.core.model.XxlJobLogGlue;
import com.xxl.job.admin.dao.XxlJobLogGlueDao;
import com.xxl.job.admin.service.XxlJobLogGlueService;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.xxl.job.admin.core.model.table.Tables.XXL_JOB_LOG_GLUE;

@Service
public class XxlJobLogGlueServiceImpl extends ServiceImpl<XxlJobLogGlueDao, XxlJobLogGlue> implements XxlJobLogGlueService {
    @Override
    public List<XxlJobLogGlue> findByJobId(int jobId) {
        return list(XXL_JOB_LOG_GLUE.JOB_ID.eq(jobId));
    }


    @Override
    public void removeOld(int jobId, int limit) {
        QueryWrapper q = QueryWrapper.create()
                .where(XXL_JOB_LOG_GLUE.ID.
                        notIn(new QueryWrapper()
                                .select(XXL_JOB_LOG_GLUE.ID).from(new QueryWrapper()
                                        .select(XXL_JOB_LOG_GLUE.ID).from(XXL_JOB_LOG_GLUE).where(XXL_JOB_LOG_GLUE.JOB_ID.eq(jobId)).orderBy(XXL_JOB_LOG_GLUE.UPDATE_TIME.desc()).limit(0,limit)
                                ))).and(XXL_JOB_LOG_GLUE.JOB_ID.eq(jobId));
        remove(q);
    }

    @Override
    public void deleteByJobId(int id) {
        remove(XXL_JOB_LOG_GLUE.JOB_ID.eq(id));
    }
}
